<?php
/**
 * 个人中心
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author     NcPHP Team
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class memberControl extends BaseHomeControl{

    public function __construct(){
        if($_GET['op']!='logout'){
            $this->_must_login = true;
        }
       parent::__construct();
    }

    /**
     * 个人中心
     */
    public function indexOp()
    {
        /* @var memberModel $memberMode */
        $memberMode = Model('member');
        $member = $memberMode->getMemberInfo(array('member_id'=>$_SESSION['member']['member_id']));
        $res = $this->check_merchant($member['mobile']);
        if($member['is_merchant']==1){
            //查询该用户是否还属于店长
            if(!$res){
                //说明店铺绑定的手机已经被改变,将此用户改为普通用户
                $memberMode->updateMember(array('member_id'=>$_SESSION['member']['member_id']),array('merchant_id'=>0,'is_merchant'=>0));
            }
        }else{
            //查询后台是否将此用户添加为商家
            if($res){
                //如果是将此用户改为商家
                $memberMode->updateMember(array('member_id'=>$_SESSION['member']['member_id']),array('merchant_id'=>$res['merchant_id'],'is_merchant'=>1));
            }
        }
        $site_phone = Model('setting')->getfby_name('site_phone','value');
        if($this->is_weixin()){
            Tpl::output('is_weixin','true');
        }
        //$data = $this->getmeber($member['mobile']);
        /*if($data['code']=='2000'){
           Tpl::output('news_member',$data['data']);
        }*/

        Tpl::output('site_phone', $site_phone);
        Tpl::output('member', $member);
        Tpl::showpage('member.index');
    }

    /**
     * 我的订单
     */
    public function orderOp()
    {
        /* @var orderModel $order_model */
        $order_model = Model('order');
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');

        $condition = array('member_id'=>$_SESSION['member']['member_id']);
        $state = isset($_GET['state']) ? intval($_GET['state']) : 0;
        switch ($state) {
            case 1:
                $condition['order_status'] = 10;
                break;
            case 2:
                $condition['order_status'] = 20;
                break;
            case 3:
                $condition['order_status'] = 21;
                break;
            case 4:
                $condition['order_status'] = 30;
                break;
        }
        $orders = $order_model->getOrderListPage($condition, '*', 10);
        foreach ($orders as $k => $order) {
            switch($order['order_type']){
                case 1:
                    $orders[$k]['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],HOTEL_MARK);
                    $orders[$k]['sku'] = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                    break;
                case 2:
                    $orders[$k]['ticket_thumb'] = UPLOAD.'ticket/'.$order['ticket_thumb'];
                    $orders[$k]['sku'] = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                    break;
                case 3:
                    $orders[$k]['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],FOOD_MARK);
                    $orders[$k]['sku'] =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
                case 4:
                    $orders[$k]['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],ITINERARY_MARK);
                    $orders[$k]['sku'] =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
            }

        }
        //var_dump($orders);
        Tpl::output('orders', $orders);
        Tpl::output('status', $order_model->status);
        Tpl::output('order_type',$order_model->type);
        //AJAX加载分页
        if (isset($_GET['isajax']) && $_GET['isajax'] == 1) {
            Tpl::showpage('member.order.page', 'null_layout');
            exit;
        }

        Tpl::output('TotalNum', pagecmd("gettotalnum"));
        Tpl::output('TotalPage', pagecmd("gettotalpage"));
        Tpl::output('_title','我的订单');
        Tpl::output('state', $state);
        Tpl::showpage('member.order');
    }

    /**
     * 订单详情
     */
    public function orderdetailOp()
    {
        /* @var orderModel $order_model */
        $order_model = Model('order');
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');
        $ordersn = $_GET['ordersn'];
        $order = $order_model->getOrderInfo(array('order_sn'=>$ordersn,'member_id'=>$_SESSION['member']['member_id']));
        switch($order['order_type']){
            case 1:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],HOTEL_MARK);
                $skus = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                break;
            case 2:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],TICKET_MARK);
                $skus = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                break;
            case 3:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],FOOD_MARK);
                $skus = Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                break;
            case 4:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],ITINERARY_MARK);
                $skus = Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                break;
        }

        Tpl::output('order', $order);
        Tpl::output('skus', $skus);
        Tpl::output('is_weixin',$this->is_weixin());
        Tpl::output('_title','订单详情');
        Tpl::showpage('member.orderdetail');
    }

    /**
     * 个人信息页面
     */
    public function infoOp()
    {
        /* @var member_infoModel $member_info_model */
        $member_info_model = Model('member_info');
        $infos = $member_info_model->getMemberInfoList(array('member_id'=>$_SESSION['member']['member_id']));
        Tpl::output('infos', $infos);
        Tpl::output('_title','我的信息');
        Tpl::showpage('member.info');
    }

    /**
     * ajax编辑个人信息
     */
    public function info_ajaxOp()
    {
        /* @var member_infoModel $member_info_model */
        $member_info_model = Model('member_info');

        $type = isset($_GET['type']) ? $_GET['type'] : '';
        switch ($type) {
            case 'get':
                $res = $member_info_model->getMemberInfo(array('id'=>intval($_GET['id'])));
                exit(json_encode(array('errcode'=>0,'errmsg'=>$res)));
            case 'add':
                $params = array(
                    'member_id' => $_SESSION['member']['member_id'],
                    'mb_name' => trim($_POST['name']),
                    'mb_phone' => trim($_POST['phone']),
                    'mb_idcard' => trim($_POST['idcard']),
                );
                if ($member_info_model->getMemberInfo(array(
                    'member_id' => $params['member_id'],
                    'mb_name' => $params['mb_name'],
                    'mb_phone' => $params['mb_phone'],
                ))) {
                    exit(json_encode(array('errcode'=>4002,'errmsg'=>'已经存在相同的信息了')));
                } else {
                    if ($member_info_model->addMemberInfo($params)) {
                        exit(json_encode(array('errcode'=>0,'errmsg'=>'')));
                    } else {
                        exit(json_encode(array('errcode'=>4001,'errmsg'=>'保存失败')));
                    }
                }
            case 'edit':
                $id = intval($_POST['id']);
                $params = array(
                    'member_id' => $_SESSION['member']['member_id'],
                    'mb_name' => trim($_POST['name']),
                    'mb_phone' => trim($_POST['phone']),
                    'mb_idcard' => trim($_POST['idcard']),
                );
                if ($member_info_model->updateMemberInfo(array('id'=>$id), $params)) {
                    exit(json_encode(array('errcode'=>0,'errmsg'=>'')));
                }
                exit(json_encode(array('errcode'=>4001,'errmsg'=>'保存失败')));
            case 'del':
                if ($member_info_model->delMemberInfo(array('id'=>intval($_POST['id'])))) {
                    exit(json_encode(array('errcode'=>0,'errmsg'=>'')));
                }
                exit(json_encode(array('errcode'=>4001,'errmsg'=>'删除失败')));
        }
    }

    /**
     * 申请成为商家
     */
    public function applyOp()
    {
        /* @var member_merchantModel $merchant_model */
        $merchant_model = Model('member_merchant');
        $member_id = $_SESSION['member']['member_id'];
        Tpl::output('_title','申请开店');
        if (chksubmit()) {
            $area_model = Model('area');
            $params = array(
                'mc_name' => $_POST['username'],
                'member_id' => $member_id,
                'mc_phone' => $_POST['phone'],
                'mc_shop_name' => $_POST['shopname'],
                'mc_cate' => $_POST['type'],
                'mc_cate_name' => Model('ticket_category')->getfby_tc_id($_POST['type'], 'tc_name'),
                'mc_apply_time' => time(),
                'mc_status' => 0,
                'province_id'=>intval($_POST['province_id']),
                'city_id'=>intval($_POST['city_id']),
                'area_id'=>intval($_POST['area_id']),
                'city'=>$area_model->getfby_area_id($_POST['city_id'],'name'),
                'district'=>$area_model->getfby_area_id($_POST['area_id'],'name'),
                'addr'=>$_POST['addr'],
                'address'=>implode('',array(
                    $area_model->getfby_area_id(intval($_POST['province_id']),'name'),
                    $area_model->getfby_area_id($_POST['city_id'],'name'),
                    $area_model->getfby_area_id(intval($_POST['area_id']),'name'),
                    $_POST['addr'],
                ))
            );

            $upload = new UploadFile();
            $upload->set('max_size', 2048);
            $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
            $result = $upload->upfile('lic');
            if($result){
                $params['mc_lic_img'] = $upload->getSysSetPath() . $upload->file_name;
            }
            $upload->file_name = '';
            $result2 = $upload->upfile('idcard');
            if($result2){
                $params['mc_idcard_img'] = $upload->getSysSetPath() . $upload->file_name;
            }
            $upload->file_name = '';
            $result2 = $upload->upfile('idcardb');
            if($result2){
                $params['mc_idcardb_img'] = $upload->getSysSetPath() . $upload->file_name;
            }
            $upload->file_name = '';
            $resul3 = $upload->upfile('mc_cover_img');
            if($resul3){
                $params['mc_cover_img'] = $upload->getSysSetPath() . $upload->file_name;
            }
            if ($merchant_model->getMerchantInfo(array('merchant_id'=>$member_id))) {
                $merchant_model->updateMerchant(array('merchant_id'=>$member_id), $params);
            } else {
                $merchant_model->addMerchant($params);
            }
            Tpl::showpage('member.applystate');
            exit;
        }

        /* @var ticket_categoryModel $cate_model */
        $cate_model = Model('ticket_category');

        $merchant = $merchant_model->getMerchantInfo(array('member_id'=>$member_id));
        if ($merchant && $merchant['mc_status'] == 0) {
            Tpl::showpage('member.applystate');
        } else {
            $cates = $cate_model->getCategoryList(array('tc_pid'=>0));
            Tpl::output('cates', $cates);
            /* @var areaModel $area_model */
            $area_model = Model('area');
            $provinces = $area_model->getAreaList(array('parent_id'=>1));
            Tpl::output('provinces',$provinces);
            /** @var serviceModel $service */
            $service = Model('service');
            $about = $service->getServiceInfo(array('service_id'=>2));
            Tpl::output('lic', $about['service_content']);
            Tpl::output('member',$_SESSION['member']);
            Tpl::showpage('member.apply');
        }
    }

    /**
     * 检查手机号是否被人申请过商家
     */
    public function _check_merchantOp()
    {
        $mobile = $_POST['mobile'];
        $name = $_POST['name'];
        if($this->check_merchant($mobile)){
            exit(json_encode((array('errcode'=>4001,'_msg'=>'该手机号已经申请过商家了'))));
        }else{
            $merchant_model = Model('member_merchant');
            $merchant_name = $merchant_model->getMerchantInfo(array('mc_shop_name'=>$name));
            if($merchant_name){
                exit(json_encode((array('errcode'=>4001,'_msg'=>'该商户已经存在'))));
            }
            exit(json_encode((array('errcode'=>4000))));
        }
    }

    /**
     * 关于我们
     */
    public function aboutOp()
    {
        /** @var serviceModel $service */
        $service = Model('service');
        $about = $service->getServiceInfo(array('service_id'=>1));
        Tpl::output('about', $about['service_content']);
        Tpl::output('_title','关于我们');
        Tpl::showpage('member.about');
    }

    /**
     * 我的收藏
     */
    public function collectOp()
    {
        //var_dump($_SESSION);exit;
        /* @var ticket_categoryModel $cate_model */
        $cate_model = Model('ticket_category');
        /** @var collectModel $collect_model */
        $collect_model = Model('collect');
        //用户ID
        $member_id = $_SESSION['member']['member_id'];
        //获取分类
        if (isset($_GET['cate'])) {
            $cate = intval($_GET['cate']);
        } else {
            $cate = 1;
        }
        //一级分类
        $cates = $cate_model->getCategoryList(array('tc_pid'=>0,'is_del'=>0));
        Tpl::output('cates', $cates);
        Tpl::output('curcate', $cate);
        //收藏的门票列表
        $list = $collect_model->getCollectListPage($member_id, $cate, 20);
        foreach ($list as $k => $v) {
            $list[$k]['tk_thumb'] = UPLOAD.'product/'.$v['tk_thumb'];
        }
        Tpl::output('tickets', $list);

        //AJAX加载分页
        if (isset($_GET['isajax']) && $_GET['isajax'] == 1) {
            Tpl::showpage('index.search.page', 'null_layout');
            exit;
        }

        Tpl::output('TotalNum', pagecmd("gettotalnum"));
        Tpl::output('TotalPage', pagecmd("gettotalpage"));
        Tpl::showpage('member.collect');
    }

    /**
     * 退出登录,调试使用
     */
    public function logoutOp()
    {
        $_SESSION = array();
        session_destroy();
        if(empty($_SESSION)){
            redirect('index.php?act=login&op=index');
        }
    }


    //获取新闻系统用户信息

    public  function getmeber($mobile)

    {
        $url = "http://lps.soshow.org/api/mobile_info?mobile=".$mobile;
        $info = $this->httpGet($url);
        return json_decode($info,true);
    }

    /**
     * 获取子级的地区
     */
    public function getAreaChildOp(){
        $parent_id = $_GET['parent_id'];
        /* @var areaModel $area_model */
        $area_model = Model('area');
        $areaList = $area_model->getAreaList(array('parent_id'=>$parent_id));
        die(json_encode($areaList));
    }



}
